package com.lsfb.duishangbaos.HomePage.Setting;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.lsfb.duishangbaos.Login.LoginActivity;
import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.LittleUtils;
import com.lsfb.duishangbaos.Utils.NoDoubleClickUtils;
import com.lsfb.duishangbaos.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.net.MD5;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.T;
import com.zgscwjm.lsfbbasetemplate.view.titlebar.LsfbTitleBar;

import java.util.HashMap;

/**
 * 修改密码
 * Created by Administrator on 2017/5/10 0010.
 */
@ContentView(R.layout.aty_amend_pwd)
public class AmendPwdActivity extends LsfbActivity {

    /**
     * 原密码
     */
    @ViewInject(R.id.aty_et_old_pwd)
    private EditText et_old_pwd;

    /**
     * 新密码
     */
    @ViewInject(R.id.aty_et_nuew_pwd)
    private EditText et_nuew_pwd;

    /**
     * 确认密码
     */
    @ViewInject(R.id.aty_et_nuew_pwd2)
    private EditText et_nuew_pwd2;


    @Override
    public void initView() {
        LsfbTitleBar titlebar = LittleUtils.setsimpletitlebar(this, "密码管理");
        TextView textView = LittleUtils.rightbtn(this, "修改");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //收起软键盘
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_old_pwd.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(et_nuew_pwd.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(et_nuew_pwd2.getWindowToken(), 0);
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    editPwd();
                }
            }
        });
        titlebar.setRightView(textView, 0, 0, DensityUtils.dp2px(this, 10), 0);

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    public void editPwd() {

        if (et_nuew_pwd.getText().toString().isEmpty()) {
            SN.SnackShow(et_nuew_pwd, "请输入原密码");
            return;
        }
        if (et_nuew_pwd.getText().toString().isEmpty()) {
            SN.SnackShow(et_nuew_pwd, "请输入新密码");
            return;
        }
        if (et_nuew_pwd2.getText().toString().isEmpty()) {
            SN.SnackShow(et_nuew_pwd, "请输入确认密码");
            return;
        }
        if (!et_nuew_pwd2.getText().toString().equals(et_nuew_pwd.getText().toString())) {
            SN.SnackShow(et_nuew_pwd, "两次密码输入不一致");
            return;
        }

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("pwd", MD5.GetMD5Code(et_nuew_pwd.getText().toString()));
        hashMap.put("opwd", MD5.GetMD5Code(et_old_pwd.getText().toString()));
        new BaseInternet().getData(URLString.EDITPWD, hashMap, AmendPwdBean.class, new AmendPwdEvent<AmendPwdBean>(), true);
    }

    @EventAnnotation
    public void AmendPwdEvent(AmendPwdEvent<AmendPwdBean> event) {
        switch (event.getCode()) {
            case 200:
                T.showShort(this, event.getMsg());
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                SN.SnackShow(et_nuew_pwd, event.getMsg());
                break;
        }
    }


}
